using Digitalmes.Application.Mes.Qulities.Defects;
using Digitalmes.Application.Mes.Qulities.Defects.Commands;
using Digitalmes.Application.Mes.Qulities.Defects.Queries;

namespace Digitalmes.WebApi.Endpoints.Mes.Qualities;

/// <summary>
/// 【质量】缺陷定义终结点。
/// </summary>
public class DefectEndpoint : EndpointGroupBase
{
    public override void Map(WebApplication app)
    {
        app.MapGroup(this, "mes/quality", "defect")
            .RequireAuthorization()
            .MapGet(GetDefectListAsync, "list")
            .MapGet(GetDefectPagedListAsync, "list-page")
            .MapPost(CreateDefectAsync)
            .MapPut(UpdateDefectAsync, "{id}")
            .MapDelete(DeleteDefectAsync, "{id}");
    }

    [EndpointDescription("获取缺陷定义信息")]
    public async Task<IApiResult> GetDefectListAsync(ISender sender, [AsParameters] GetDefectListQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("分页获取缺陷定义信息")]
    public async Task<IApiResult> GetDefectPagedListAsync(ISender sender, [AsParameters] GetDefectListWithPagedQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("创建缺陷定义")]
    public async Task<IApiResult> CreateDefectAsync(ISender sender, QcDefectDefineDto dto)
    {
        var result = await sender.Send(new CreateDefectCommand(dto));
        return result.ToApiResult();
    }

    [EndpointDescription("更新缺陷定义")]
    public async Task<IApiResult> UpdateDefectAsync(ISender sender, long id, QcDefectDefineDto dto)
    {
        var result = await sender.Send(new UpdateDefectCommand(id, dto));
        return result.ToApiResult();
    }

    [EndpointDescription("删除缺陷定义")]
    public async Task<IApiResult> DeleteDefectAsync(ISender sender, long id)
    {
        var result = await sender.Send(new DeleteDefectCommand(id));
        return result.ToApiResult();
    }
}
